package com.zhangxu.microservice.auth.facade.service.impl;

import cn.hutool.json.JSONUtil;
import com.zhangxu.microservice.auth.facade.service.EmployeeFacadeService;
import com.zhangxu.microservice.common.exception.BizException;
import com.zhangxu.microservice.common.result.CommonResult;
import com.zhangxu.microservice.employee.api.feign.EmployeeClient;
import com.zhangxu.microservice.employee.api.response.EmployeeApiDTO;
import com.zhangxu.microservice.employee.api.response.EmployeeAuthDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class EmployeeFacadeServiceImpl implements EmployeeFacadeService {

    private final EmployeeClient employeeClient;

    @Override
    public EmployeeAuthDTO getEmployeeByAccount(String account) {
        log.info("请求EmployeeClient#getByAccount,account:{}", account);
        CommonResult<EmployeeAuthDTO> result = employeeClient.getByAccount(account);
        log.info("请求EmployeeClient#getByAccount,返回:{}", JSONUtil.toJsonStr(result));
        if (!result.checkSuccess()) {
            throw new BizException(result.getErrorMsg());
        }
        return result.getData();
    }

    @Override
    public EmployeeApiDTO getEmployeeById(Long employeeId) {
        log.info("请求EmployeeClient#getEmployeeById,employeeId:{}", employeeId);
        CommonResult<EmployeeApiDTO> result = employeeClient.getById(employeeId);
        log.info("请求EmployeeClient#getEmployeeById,返回:{}", JSONUtil.toJsonStr(result));
        if (!result.checkSuccess()) {
            throw new BizException(result.getErrorMsg());
        }
        return result.getData();
    }

    @Override
    public List<EmployeeApiDTO> listEmployeeByIds(List<Long> employeeIds) {
        log.info("请求EmployeeClient#listByIds,employeeIds:{}", employeeIds);
        CommonResult<List<EmployeeApiDTO>> result = employeeClient.listByIds(employeeIds);
        log.info("请求EmployeeClient#listByIds,返回:{}", JSONUtil.toJsonStr(result));
        if (!result.checkSuccess()) {
            throw new BizException(result.getErrorMsg());
        }
        return result.getData();
    }
}